package com.sls.game.parser.nutsnet;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.log4j.Logger;
import org.dom4j.DocumentException;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.sls.game.pojo.NutsNetBalanceResInfo;

/**
 * 
 * @description 以SAX方式解析xml字符串(合作伙伴用户Sup余额查询接口)
 * @author Wangxuegang
 * @date 2012-9-4
 * 
 */
public class BalanceResponseSaxParseXml extends DefaultHandler {
	private List<NutsNetBalanceResInfo> balanceResList = null;

	private NutsNetBalanceResInfo balanceRes;

	// 用来存放每次遍历后的元素名称(节点名称)
	private String tagName;

	private static Logger log = Logger
			.getLogger(BalanceResponseSaxParseXml.class);

	/**
	 * 对返回的xml格式字符串数据进行解析
	 * 
	 * @param resultStr
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 * @throws SAXException
	 * @throws ParserConfigurationException
	 * @throws DocumentException
	 */
	public static List<NutsNetBalanceResInfo> parseXmlString(String xmlStr)
			throws UnsupportedEncodingException {
		String encodeXmlStr = EncodeChineseCode.encodeChineseCodeOfGBK(xmlStr);
		// 加载资源文件 转化为一个输入流
		byte[] xmlStrByte = encodeXmlStr.getBytes();
		ByteArrayInputStream in = new ByteArrayInputStream(xmlStrByte);
		try {
			// 构建SAXParser
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser parser = factory.newSAXParser();

			BalanceResponseSaxParseXml handler = new BalanceResponseSaxParseXml();
			parser.parse(in, handler);
			return handler.getBalanceResList();

		} catch (ParserConfigurationException ep) {
			log.error(ep);
		} catch (SAXException es) {
			log.error(es);
		} catch (IOException e) {
			log.error(e);
		}

		return null;
	}

	// 只调用一次 初始化list集合
	@Override
	public void startDocument() throws SAXException {
		balanceResList = new ArrayList<NutsNetBalanceResInfo>();
	}

	// 调用多次 开始解析
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if (qName.equals("response")) {
			balanceRes = new NutsNetBalanceResInfo();
		}
		this.tagName = qName;
	}

	// 调用多次
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (qName.equals("response")) {
			this.balanceResList.add(this.balanceRes);
		}
		this.tagName = null;
	}

	// 只调用一次
	@Override
	public void endDocument() throws SAXException {
	}

	// 调用多次
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if (this.tagName != null) {
			String data = new String(ch, start, length);
			if (this.tagName.equals("supUserId")) {
				this.balanceRes.setSupUserId(data);
			} else if (this.tagName.equals("balance")) {
				this.balanceRes.setBalance(data);
			} else if (this.tagName.equals("failedCode")) {
				this.balanceRes.setFailedCode(data);
			} else if (this.tagName.equals("failedReason")) {
				try {
					data = URLDecoder.decode(data, "GBK");
				} catch (UnsupportedEncodingException e) {
					log.error(e);
				}
				this.balanceRes.setFailedReason(data);
			}
		}

	}

	public List<NutsNetBalanceResInfo> getBalanceResList() {
		return balanceResList;
	}

	public void setBalanceResList(List<NutsNetBalanceResInfo> balanceResList) {
		this.balanceResList = balanceResList;
	}

	public NutsNetBalanceResInfo getBalanceRes() {
		return balanceRes;
	}

	public void setBalanceRes(NutsNetBalanceResInfo balanceRes) {
		this.balanceRes = balanceRes;
	}

	public String getTagName() {
		return tagName;
	}

	public void setTagName(String tagName) {
		this.tagName = tagName;
	}

	// public static void main(String[] args) throws
	// UnsupportedEncodingException {
	// String xmlStr = "<?xml version=\"1.0\" encoding=\"GBK\"?>"
	// + "<response>" + "<supUserId>aa</supUserId>"
	// + "<balance>bb</balance>" + "<failedCode>cc</failedCode>"
	// + "<failedReason>中华人面</failedReason>"
	// + "</response>";
	// String str = EncodeChineseCode.encodeChineseCode(xmlStr);
	// System.out.println(str); //%CD%F8%C2%E7%B3%AC%CA%B1
	// List<NutsNetBalanceResInfo> balanceResList = parseXmlString(str);
	// if (balanceResList.size() > 0) {
	// for (NutsNetBalanceResInfo resInfo : balanceResList) {
	// log.info(resInfo.getSupUserId());
	// log.info(resInfo.getBalance());
	// log.info(resInfo.getFailedCode());
	// log.info(URLDecoder.decode(resInfo.getFailedReason(), "GBK"));
	// }
	// }
	// }

}
